//
//  DrawView.m
//  Quartz_2D
//
//  Created by 吴志勇 on 2020/3/2.
//  Copyright © 2020 wzy. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView




- (void)awakeFromNib{
    
    //注意:drawRect 如果是手动调用的话，他是h不会给你创建跟View相关联的上下文
    //只有系统调用该方法时，才会创建跟View相关联的上下文
//    [self drawRect:self.bounds];
    
    //重绘(系统自动帮你调用drawRect:)
//    [self setNeedsDisplay];
}




/**
 作用:专门用来绘图
 什么时候调用:当View即将显示的时候调用
 @param rect 当前View的bounds
 */
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
   //画弧线
    //Center:弧线所在的圆心
    //radius:圆的半径
    //startAngle:开始角度
    //startAngle:截止角度
    //clockwise:yes: 顺时针 no：逆时针
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(100, 100) radius:10 startAngle:M_PI_4 endAngle:0 clockwise:YES];
    
//    //关闭路径：从路径终点连接一根线到路径的终点
//    [path closePath];
    //会自动关闭路径
    [path fill];
    [path stroke];
    
  

}


- (void)drawOval{
    //画椭圆
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(20, 20, 200, 100)];
    [path fill];
    
    [path stroke];
}


- (void)drawRect{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //绘制矩形
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 30, 30)];
    CGContextAddPath(ctx, path.CGPath);
    
    CGContextFillPath(ctx);
}

- (void)drawCurve{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.绘制路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //2.1设置起点
    [path moveToPoint:CGPointMake(50, 200)];
    //2.2添加一根曲线到终点，controlPoint:控制点
    [path addQuadCurveToPoint:CGPointMake(250, 280) controlPoint:CGPointMake(50, 60)];
    //3.把绘制的内容保存到上下文
    CGContextAddPath(ctx, path.CGPath);
    //4.把上下文的内容显示到View上
    CGContextStrokePath(ctx);
    
}

- (void)drawLine{
     //1.获取上下文
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    //2.绘制路径
    UIBezierPath *path = [UIBezierPath bezierPath];
    //2.1设置起点
    [path moveToPoint:CGPointMake(50, 200)];
    //2.2添加一根线到终点
    [path addLineToPoint:CGPointMake(280, 50)];
    
    
    //    //把上一条线的终点当做起点
    //    [path addLineToPoint:CGPointMake(200, 280)];
    
    
    //上下文的状态
    //设置线的宽度
    CGContextSetLineWidth(ctx, 10);
    //s设置线的连接样式
    CGContextSetLineJoin(ctx, kCGLineJoinRound);
    //设置线的顶角样式
    CGContextSetLineCap(ctx, kCGLineCapRound);
    
    //设置颜色
    [[UIColor redColor] set];
    
    
    //3.把绘制的内容保存的上下文当中
    CGContextAddPath(ctx, path.CGPath);
    //4.把上下文的内容显示到View上(渲染到View的layer上)
    CGContextStrokePath(ctx);
}


@end
